package com.eco.fanliapp.pinduoduo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.utils.*;
import com.eco.fanliapp.vo.PddGoodsSearchResponse;
import com.eco.fanliapp.vo.respone.PDDGoodsPromotionUrlResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;


@Service
@Slf4j
public class PDDSearchController {

    public static void main(String[] args) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        Long time = Long.valueOf(timestamp);
        paraMap.put("type", PDDConfig.PDD_PROMURL_METHOD);
        paraMap.put("data_type", PDDConfig.PDD_DATA_TYPE);
        paraMap.put("timestamp", time.toString());
        paraMap.put("client_id", PDDConfig.PDD_CLIENT_ID);
        paraMap.put("p_id", PDDConfig.PDD_PID);
        List<Long> list = new ArrayList<>();
        list.add(8229899731L);
        paraMap.put("goods_id_list", list.toString());

        StringBuilder url = new StringBuilder(PDDConfig.PDD_HOME_URL);
        url = url.append("?type=").append(PDDConfig.PDD_PROMURL_METHOD)
                .append("&data_type=").append(PDDConfig.PDD_DATA_TYPE)
                .append("&timestamp=").append(time)//时间戳
                .append("&client_id=").append(PDDConfig.PDD_CLIENT_ID)
                .append("&p_id=").append(PDDConfig.PDD_PID)
                .append("&goods_id_list=").append(list.toString())
                .append("&sign=").append(SignMD5Util.getPDDSignStr(paraMap, PDDConfig.PDD_CLIENT_SECRET));
        String pddSearch = HttpUtils.sendGet(url.toString(), "");
        JSONObject object = JSONObject.parseObject(pddSearch);
        String a = object.getString("goods_promotion_url_generate_response");
        PDDGoodsPromotionUrlResponse response = JSON.parseObject(a, PDDGoodsPromotionUrlResponse.class);
        System.out.println(pddSearch);
    }

    public PddGoodsSearchResponse PddGoodsSearch(String keyword, Long pageNum, Long pageSize, String sort, Boolean hasCoupon) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        Long time = Long.valueOf(timestamp);
        paraMap.put("type", PDDConfig.PDD_SEARCH_METHOD);
        paraMap.put("data_type", PDDConfig.PDD_DATA_TYPE);
        paraMap.put("timestamp", time.toString());
        paraMap.put("client_id", PDDConfig.PDD_CLIENT_ID);
        paraMap.put("keyword", keyword);
        paraMap.put("page", pageNum.toString());
        paraMap.put("page_size", pageSize.toString());
        paraMap.put("sort_type", sort);
        if (hasCoupon != null) {
            paraMap.put("with_coupon", hasCoupon.toString());
        }
        paraMap.put("pid", PDDConfig.PDD_PID);

        StringBuilder url = new StringBuilder(PDDConfig.PDD_HOME_URL);
        url = url.append("?type=").append(PDDConfig.PDD_SEARCH_METHOD)
                .append("&data_type=").append(PDDConfig.PDD_DATA_TYPE)
                .append("&timestamp=").append(time)//时间戳
                .append("&client_id=").append(PDDConfig.PDD_CLIENT_ID)
                .append("&keyword=").append(keyword)
                .append("&page=").append(pageNum.toString())//页数
                .append("&page_size=").append(pageSize.toString())//条数;
                .append("&pid=").append(PDDConfig.PDD_PID)
                .append("&sort_type=").append(sort);
        if (hasCoupon != null) {
            url.append("&with_coupon=").append(hasCoupon.toString());
        }
        url.append("&sign=").append(SignMD5Util.getPDDSignStr(paraMap, PDDConfig.PDD_CLIENT_SECRET));
        try {
            String pddSearch = HttpUtils.sendGet(url.toString(), "");
            JSONObject object = JSONObject.parseObject(pddSearch);
            String a = object.getString("goods_search_response");
            PddGoodsSearchResponse response = JSON.parseObject(a, PddGoodsSearchResponse.class);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public String PddGoodsPromUrl(Long goodsId, AppUser appUser) {
        String customParameters = null;
        if (appUser != null && appUser.getUserId() != null) {
            customParameters = appUser.getUserId().toString();
        } else {
            customParameters = "0";
        }
        TreeMap<String, String> paraMap = new TreeMap<>();
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        Long time = Long.valueOf(timestamp);
        paraMap.put("type", PDDConfig.PDD_PROMURL_METHOD);
        paraMap.put("data_type", PDDConfig.PDD_DATA_TYPE);
        paraMap.put("timestamp", time.toString());
        paraMap.put("client_id", PDDConfig.PDD_CLIENT_ID);
        paraMap.put("p_id", PDDConfig.PDD_PID);
        List<Long> list = new ArrayList<>();
        list.add(goodsId);
        paraMap.put("goods_id_list", list.toString());
        paraMap.put("custom_parameters", customParameters);


        StringBuilder url = new StringBuilder(PDDConfig.PDD_HOME_URL);
        url = url.append("?type=").append(PDDConfig.PDD_PROMURL_METHOD)
                .append("&data_type=").append(PDDConfig.PDD_DATA_TYPE)
                .append("&timestamp=").append(time)//时间戳
                .append("&client_id=").append(PDDConfig.PDD_CLIENT_ID)
                .append("&p_id=").append(PDDConfig.PDD_PID)
                .append("&goods_id_list=").append(list.toString())
                .append("&custom_parameters=").append(customParameters)
                .append("&sign=").append(SignMD5Util.getPDDSignStr(paraMap, PDDConfig.PDD_CLIENT_SECRET));
        try {
            String pddSearch = HttpUtils.sendGet(url.toString(), "");
            JSONObject object = JSONObject.parseObject(pddSearch);
            String a = object.getString("goods_promotion_url_generate_response");
            PDDGoodsPromotionUrlResponse response = JSON.parseObject(a, PDDGoodsPromotionUrlResponse.class);
            String PromotionUrl = null;
            if (response != null && response.getGoods_promotion_url_list().length > 0) {
                PromotionUrl = response.getGoods_promotion_url_list()[0].getUrl();
            }
            return PromotionUrl;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
